﻿/* ******************************************************** */
/* Arcans Project (2010)                                    */
/* Jeu sous forme d'application web entièrement conçu       */
/* et réalisé par Killian Le Maitre                         */
/* Jeu sous licence GNU GPL v3 (voir NOTICE et COPYING)     */
/* ******************************************************** */

//variables globales ******************************************************************************
var canal = 1;
//gestion du chat *********************************************************************************
var bl_chat = {
	//méthodes :
		//ouverture d'un canal
	ouverture: function (res) {
		var div_center = document.createElement("div");
		zone_panel.appendChild(div_center);
		//construction du menu du chat
		var tab_menu = document.createElement("table");
		tab_menu.setAttribute("id", "chat_menu");
		div_center.appendChild(tab_menu);
		bl_chat.gest_menu();
		//construction du canal
		var div_canal = document.createElement("div");
		div_canal.setAttribute("id", "chat_div");
		div_center.appendChild(div_canal);
		if (canal == 1) ajaxFun("acanc",1,new Array("1"),bl_chat.gest_canal);
		if (canal == 2) ajaxFun("acanc",1,new Array("2"),bl_chat.gest_canal);
		if (canal == 3) ajaxFun("acanc",1,new Array("3"),bl_chat.gest_canal);
		//construction du champs input / pied du chat
		var foot_p = document.createElement("p");
		foot_p.setAttribute("id", "chat_foot");
		var crea_input = document.createElement("input");
		crea_input.setAttribute("id", "chat_input");
		var crea_button = document.createElement("button");
		crea_button.setAttribute("onclick", "javascript:bl_chat.envoi();");
		crea_button.appendChild(document.createTextNode("Envoyer"));
		foot_p.appendChild(crea_input);
		foot_p.appendChild(crea_button);
		div_center.appendChild(foot_p);
	},
		//changement de canal
	canal_change: function (choix) {
		canal = choix;
		var chat_div = document.getElementById("chat_div");
		while (chat_div.hasChildNodes()) chat_div.removeChild(chat_div.firstChild);
		bl_chat.gest_menu();
		if (canal == 1) ajaxFun("acanc",1,new Array("1"),bl_chat.gest_canal);
		if (canal == 2) ajaxFun("acanc",1,new Array("2"),bl_chat.gest_canal);
		if (canal == 3) ajaxFun("acanc",1,new Array("3"),bl_chat.gest_canal);
	},
		//création et modification du menu
	gest_menu: function () {
		var chat_menu = document.getElementById("chat_menu");
		while (chat_menu.hasChildNodes()) chat_menu.removeChild(chat_menu.firstChild);
		var row_menu = document.createElement("tr");
		var cel_menu = document.createElement("td");
		if (canal == 1) {
			cel_menu.setAttribute("class", "active");
			cel_menu.appendChild(document.createTextNode("Général"));
		}
		else {
			cel_menu.setAttribute("class", "underline");
			var crea_button = document.createElement("button");
			crea_button.setAttribute("onclick", "javascript:bl_chat.canal_change(1);");
			crea_button.appendChild(document.createTextNode("Général"));
			cel_menu.appendChild(crea_button);
		}
		row_menu.appendChild(cel_menu);
		var cel_menu = document.createElement("td");
		if (canal == 2) {
			cel_menu.setAttribute("class", "active");
			cel_menu.appendChild(document.createTextNode("Corporation"));
		}
		else {
			cel_menu.setAttribute("class", "underline");
			var crea_button = document.createElement("button");
			crea_button.setAttribute("onclick", "javascript:bl_chat.canal_change(2);");
			crea_button.appendChild(document.createTextNode("Corporation"));
			cel_menu.appendChild(crea_button);
		}
		row_menu.appendChild(cel_menu);
		var cel_menu = document.createElement("td");
		if (canal == 3) {
			cel_menu.setAttribute("class", "active");
			cel_menu.appendChild(document.createTextNode("Privé"));
		}
		else {
			cel_menu.setAttribute("class", "underline");
			var crea_button = document.createElement("button");
			crea_button.setAttribute("onclick", "javascript:bl_chat.canal_change(3);");
			crea_button.appendChild(document.createTextNode("Privé"));
			cel_menu.appendChild(crea_button);
		}
		row_menu.appendChild(cel_menu);
		cel_menu = document.createElement("td");
		cel_menu.setAttribute("class", "underline");
		row_menu.appendChild(cel_menu);
		chat_menu.appendChild(row_menu);
	},
		//gestion et modification en création du canal
	gest_canal: function (res) {
		var chat_div = document.getElementById("chat_div");
		if (canal == 1) {
			var infos = res.getElementsByTagName("infos").item(0);
			if (infos.getAttribute("value") == 0) {
				var crea_div = document.createElement("span");
				crea_div.setAttribute("id", "0");
				crea_div.appendChild(document.createTextNode("Il n'y a aucune entrée pour cette discussion."));
				chat_div.appendChild(crea_div);
			}
			else if (infos.getAttribute("value") == "new") {
				var lines = res.getElementsByTagName("line");
				for (var i = 0; i < lines.length; i++) {
					var crea_div = document.createElement("div");
					crea_div.setAttribute("id", lines[i].getAttribute("id"));
					var crea_span = document.createElement("span");
					crea_span.appendChild(document.createTextNode(lines[i].getAttribute("heure") + " "));
					crea_div.appendChild(crea_span);
					crea_span = document.createElement("span");
					crea_span.setAttribute("class", "users");
					crea_span.appendChild(document.createTextNode(lines[i].getAttribute("user") + " : "));
					crea_div.appendChild(crea_span);
					crea_span = document.createElement("span");
					crea_span.appendChild(document.createTextNode(lines[i].firstChild.data));
					crea_div.appendChild(crea_span);
					chat_div.appendChild(crea_div);
				}
			}
			else if (infos.getAttribute("value") == "add") {
				var chat_div = document.getElementById("chat_div");
				if (chat_div.getElementsByTagName("span").length == 1) chat_div.removeChild(chat_div.firstChild);
				var taille = chat_div.getElementsByTagName("div").length + res.getElementsByTagName("line").length;
				if (taille > 20) {
					for (var i = 0; i < (taille - 20); i++) chat_div.removeChild(chat_div.firstChild);
				}
				var lines = res.getElementsByTagName("line");
				for (var i = 0; i < lines.length; i++) {
					var crea_div = document.createElement("div");
					crea_div.setAttribute("id", lines[i].getAttribute("id"));
					var crea_span = document.createElement("span");
					crea_span.appendChild(document.createTextNode(lines[i].getAttribute("heure") + " "));
					crea_div.appendChild(crea_span);
					crea_span = document.createElement("span");
					crea_span.setAttribute("class", "users");
					crea_span.appendChild(document.createTextNode(lines[i].getAttribute("user") + " : "));
					crea_div.appendChild(crea_span);
					crea_span = document.createElement("span");
					crea_span.appendChild(document.createTextNode(lines[i].firstChild.data));
					crea_div.appendChild(crea_span);
					chat_div.appendChild(crea_div);
				}
			}
			else if (infos.getAttribute("value") == "no") {}
			window.clearTimeout();
			window.setTimeout("bl_chat.maj();",2000);
		}
		else chat_div.appendChild(document.createTextNode("Cette partie n'est pas encore intégrée dans la version actuelle."));
		//********************************************************************************
		//********************************************************************************
		//gestion du bouton de mail
	},
		//envoi de message
	envoi: function () {
		if (canal == 1) {
			var texte = document.getElementById("chat_input").value;
			if (texte != "") {
				var id = document.getElementById("chat_div").lastChild.getAttribute("id");
				ajaxFun("amesschat", 2,new Array(texte,id),bl_chat.gest_canal);
				document.getElementById("chat_input").value = "";
			}
		}
		else alert("Partie en cours de construction");
		//********************************************************************************
		//********************************************************************************
	},
		//mise à jour du canal
	maj: function () {
		var div = document.getElementById("chat_div");
		if (div != null) ajaxFun("amajchat",1,new Array(div.lastChild.getAttribute("id")),bl_chat.gest_canal);
	}
}